package com.yds.dccjll.lib.base.webview;

import android.annotation.SuppressLint;
import android.content.Context;
import android.net.Uri;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebView;


public class ReWebChomeClient extends WebChromeClient {

    private final String tag = ReWebChomeClient.class.getSimpleName();

    private Boolean isProgressShow = false;
    private Context mContext;
    private OpenFileChooserCallBack mOpenFileChooserCallBack;

    public ReWebChomeClient(Context mContext, OpenFileChooserCallBack mOpenFileChooserCallBack) {
        this.mContext = mContext;
        this.mOpenFileChooserCallBack = mOpenFileChooserCallBack;
    }

    //For Android 3.0+
    public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
        mOpenFileChooserCallBack.openFileChooserCallBack(uploadMsg, acceptType);
    }

    // For Android < 3.0
    public void openFileChooser(ValueCallback<Uri> uploadMsg) {
        openFileChooser(uploadMsg, "");
    }

    // For Android  > 4.1.1
    public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
        openFileChooser(uploadMsg, acceptType);

    }

    public interface OpenFileChooserCallBack {
        void openFileChooserCallBack(ValueCallback<Uri> uploadMsg, String acceptType);
    }

    @Override
    @SuppressLint("NewApi")
    public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {
        String acceptTypes[] = fileChooserParams.getAcceptTypes();
        String acceptType = "";
        for (String acceptType1 : acceptTypes) {
            if (acceptType1 != null && acceptType1.length() != 0)
                acceptType += acceptType1 + ";";
        }
        if (acceptType.length() == 0) {
            acceptType = "*/*";
        }

        final ValueCallback<Uri[]> finalFilePathCallback = filePathCallback;
        ValueCallback<Uri> vc = new ValueCallback<Uri>() {
            @Override
            public void onReceiveValue(Uri value) {
                Uri[] result;
                if (value != null)
                    result = new Uri[]{value};
                else
                    result = null;
                finalFilePathCallback.onReceiveValue(result);
            }
        };
        openFileChooser(vc, acceptType, "filesystem");
        return true;
    }


}
